<?php

namespace App\Admin\Metrics\Examples;

use App\Models\Administrator;
use App\Models\Clue;
use Carbon\Carbon;
use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Line;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class NewUsers extends Line
{
    public $type;

    /**
     * @param $type
     */
    public function __construct($type = null)
    {
        $this->type = $type;
        parent::__construct();
    }


    /**
     * 初始化卡片内容
     *
     * @return void
     */
    protected function init()
    {
        parent::init();

        $this->title($this->type === 2 ? '我的线索数' : '线索总数');
        $this->dropdown([
            '7' => '近 7 天',
        ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        $order = 'ASC';
        $endDate = Carbon::today();


        $option = $request->get('option',7);
        switch ($option) {
            case '30':
                $startDate = Carbon::today()->addMonths(-1);
                break;
            case '28':
                $startDate = Carbon::today()->addDays(-28);
                break;
            case '7':
            default:
                $startDate = Carbon::today()->addDays(-7);
        }

        $dateInc = ($order == 'DESC') ? -1 : 1;
        $dateCycleHolder = clone($dateInc > 0 ? $startDate : $endDate);
        $dateCycleEnd = clone($dateInc > 0 ? $endDate : $startDate);


        $query = Clue::query()
            ->select(array(
                'user_id',
                DB::raw('DATE(`post_date`) as `date`'),
                DB::raw('COUNT(*)as `count`')
            ))
            ->where('post_date', '>', $startDate);

        $user = Admin::user();
        $type = $user->can("clues.check_all") ? 1 : ($user->isRole(Administrator::DISPATCH_KEY) ? 2 : 0);
        if ($type === 2) {
            $query->where('user_id', Admin::user()->id);
        }


        $posts = $query
            ->orderBy('date', $order)
            ->groupBy('date')
            ->pluck('count', 'date');

        $postsFinal = collect();

        $total = $posts->sum();

        while ($dateCycleHolder->ne($dateCycleEnd)) {
            $dateCurr = $dateCycleHolder->format('Y-m-d');
            $postsFinal->put($dateCurr, $posts->get($dateCurr, 0));
            $dateCycleHolder->addDay($dateInc);
        }
        $dateCurr = $dateCycleHolder->format('Y-m-d');
        $postsFinal->put($dateCurr, $posts->get($dateCurr, 0));

        $todayCount = $postsFinal->get(today()->toDateString());
        $str = "今天: {$todayCount}条<br> 近{$option}天 : {$total}条";

        // 卡片内容
        $this->withContent($str);
        // 图表数据
        $this->withChart($postsFinal->values()->toArray());
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => [
                [
                    'name' => $this->title,
                    'data' => $data,
                ],
            ],
        ]);
    }

    /**
     * 设置卡片内容.
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
<div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
    <h4 class="ml-1 ">{$content}</h4>
    <span class="mb-0 mr-1 text-80">{$this->title}</span>
</div>
HTML
        );
    }
}
